package com.valuedaas.schoolsafety.service;

import com.alibaba.fastjson.JSON;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import com.valuedaas.schoolsafety.common.collects.OrgUnit;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Service 基类
 */
@Service
public abstract class BaseService {

    private static Logger LOG = LoggerFactory.getLogger(BaseService.class);

    @Autowired
    protected MongoTemplate mongoTemplate;

    protected abstract String collName();

//===========================获取单条记录==============================

    /**
     * 根据id获取一条记录
     *
     * @param id
     * @return
     */
    public Document get(String id) {
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        return mongoTemplate.getCollection(collName()).find(new Document("_id", new ObjectId(id))).first();
    }

    /**
     * 根据id获取一条记录
     *
     * @param id
     * @return
     */
    public Document get(ObjectId id) {
        return mongoTemplate.getCollection(collName()).find(new Document("_id", id)).first();
    }

    /**
     * 根据条件获取一条记录
     *
     * @param filter
     * @return
     */
    public Document get(Bson filter) {
        return mongoTemplate.getCollection(collName()).find(filter).first();
    }

    /**
     * 根据条件获取一条记录
     *
     * @param filter
     * @return
     */
    public Document get(Document filter) {
        return mongoTemplate.getCollection(collName()).find(filter).first();
    }

}
